package com.github.elliot.gatewaycenter.function;

import org.apache.commons.lang3.StringUtils;

import java.util.function.BiPredicate;
import java.util.regex.Pattern;

/**
 * 路径匹配断言器
 */
public class PathMatchPredicate implements BiPredicate<String, String> {

    @Override
    public boolean test(String path, String pattern) {
        if(StringUtils.isBlank(path) || StringUtils.isBlank(pattern)){
            return false;
        }
        path = path.toLowerCase();
        pattern = pattern.toLowerCase();
        pattern = "^" + pattern
                .replace("/*/", "/[^/]*/")
                .replace("/**", "/.*")
                .replace("/*", "/[^/]*");
        return Pattern.matches(pattern, path);
    }
}
